<?php

namespace app\controller;

use app\BaseController;
use app\service\NetworkBussinessService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 网点业务管理
 */
class Networkbussiness extends BaseController
{
    /**
     * @desc 网点业务信息
     * @return void
     */
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['uid'] = $uid;
        $info = NetworkBussinessService::listsInfo($param);
        returnSuccess(200, '获取成功', $info);
    }

    //新增网点信息
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['cost_money'] = Request::param('cost_money/s', '');
        $param['cost_time'] = Request::param('cost_time/s', '');
        $param['cooperation_startime'] = Request::param('cooperation_startime/s', '');
        $param['cooperation_endtime'] = Request::param('cooperation_endtime/s', '');
        $param['tran_rules_id'] = Request::param('tran_rules_id/s', '');
        $time_requirements = Request::param('time_requirements/s', '');
        //time_requirements -----[{"start_time":"2021-1-1","end_time":"2021-1-1"},{"start_time":"2021-1-1","end_time":"2021-1-1"},{"start_time":"2021-1-1","end_time":"2021-1-1"}]
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\NetworkBussinessValidate::class)->check([
                'name' => $param['name'],
                'business_type_id' => $param['business_type_id'],
                'cost_money' => $param['cost_money'],
                'cost_time' => $param['cost_time'],
                'cooperation_startime' => $param['cooperation_startime'],
                'cooperation_endtime' => $param['cooperation_endtime'],
                'tran_rules_id' => $param['tran_rules_id'],
                'time_requirements' => $time_requirements,
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        // $time_requirementsRes=json_decode($time_requirements,true);
        $data = NetworkBussinessService::add($param, $logParam, $time_requirements);
        if ($data == 10000) {
            returnSuccess(200, '添加成功', $data);
        } elseif ($data == 10005) {
            returnFail(21103, '数据格式错误');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改司机信息
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['cost_money'] = Request::param('cost_money/s', '');
        $param['cost_time'] = Request::param('cost_time/s', '');
        $param['cooperation_startime'] = Request::param('cooperation_startime/s', '');
        $param['cooperation_endtime'] = Request::param('cooperation_endtime/s', '');
        $param['tran_rules_id'] = Request::param('tran_rules_id/s', '');
        $time_requirements = Request::param('time_requirements/s', '');
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\NetworkBussinessValidate::class)->check([
                'name' => $param['name'],
                'business_type_id' => $param['business_type_id'],
                'cost_money' => $param['cost_money'],
                'cost_time' => $param['cost_time'],
                'cooperation_startime' => $param['cooperation_startime'],
                'cooperation_endtime' => $param['cooperation_endtime'],
                'tran_rules_id' => $param['tran_rules_id'],
                'time_requirements' => $time_requirements,
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        // $time_requirementsRes=json_decode($time_requirements,true);
        $data = NetworkBussinessService::edit($param, $logParam, $id, $time_requirements);
        if ($data == 10000) {
            returnSuccess(200, '修改成功', $data);
        } elseif ($data == 10005) {
            returnFail(21103, '数据格式错误');
        } elseif ($data == 10006) {
            returnFail(21104, '内部错误');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除网点业务信息
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = NetworkBussinessService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功', $data);
        } else {
            returnFail(21101, '添加失败');
        }
    }
}
